
** this file computes the travel time on the fastest routes without the Panama Canal. 



clear all
global main "/Users/ingahei/UiO Dropbox/IngaB Heiland/Ais project/Repository"
cd "$main"

global data "$main/data_intermediate"
global input "$data/input514NP"
global output "$data/output514NP"
global soutput "$data/server_outputNP"
global figures "figures"
global aoutput "$data/output514"



*** prepare files to be transfered to server


forvalues i=1(1)514 {
u "$data/trips_hrs", clear
keep if D_a==`i'
merge m:1 D_a A_ac using "$data/XPanama_ids_small"
keep if _merge==1 // keep only routes not passing PC
drop _merge
compress
save "$input/trips_hrs`i'", replace
}


forvalues i=1(1)514 {
u "$data/input514/trips_Thrs`i'", clear
merge m:1 D_a A_ac using "$data/XPanama_ids_small"
keep if _merge==1 // keep only routes not passing PC
drop _merge
compress
save "$input/trips_Thrs`i'", replace
}



** process files on server using routes_hrs4sagaNP.do 



**** process output from server


local files: dir "$soutput" files "*.dta"

foreach file of loc files {


*local file "hrs1_12"
u "$soutput/`file'", clear 
	
rename D_a D15_acid
rename A_a D16_acid	

drop if D0_a == D16_a

if _N>0 {

sort D16_a A_t D0_t
local l=1
while `l'>0 {
bysort D16_a: drop if D0_time[_n]<D0_time[_n-1] & D0_time[_n-1]!=.
local l = r(N_drop)
}


sort D0_a D16_a D0_t
bysort D0_a D16_ac: g n=_n

g DEP=D0_a
g ARR=D16_a

save "$output/NPfull`file'", replace
}
}



***** generate figure with additional days needed to avoid Panama Canal  



forvalues p=1(1)514 {

di "`p'"

u "$aoutput/fullhrs`p'_12", clear

reshape long D@_acid, i(DEP ARR *time n) j(hop)
** drop superfluous hops
sort DEP ARR n hop
bysort DEP ARR  n: drop if D_acid==D_acid[_n-1] & D_acid[_n-1]!=.
bysort DEP ARR  n: g A_acid = D_acid[_n+1]
merge m:1 D_acid A_acid using "$data/XPanama_ids_small"
drop if _merge==2
drop _merge
egen XP=max(XP), by(DEP ARR D0_t A_t n)
replace XP=0 if XP==.
g NP =0
keep if hop==0
keep DEP ARR *time NP XP

append using "$output/NPfullhrs`p'_12"

keep if A_time<184*24


if _N>0 {

keep DEP ARR *time NP XP
replace NP=1 if NP==.
replace XP=0 if NP==1

duplicates drop

* if route with identical departure and arrival time and different XP exist in original data, drop non-passing route --> this one will be found in the NP dataset
duplicates tag DEP ARR D0 NP, g(tag)
drop if tag==1 & XP==0

drop tag

reshape wide A_t XP, i(DEP ARR D0) j(NP)

* compute travel time + wait time in origin port for routes with PC on dates where a non-PC routes leaves and but no PC route and vice versa
gsort DEP ARR -D0 
bysort DEP ARR: carryforward A_time1, gen(A_time1new)
bysort DEP ARR: carryforward A_time0, gen(A_time0new)
* also apply XP of the next possible route (only relevant for XP0)
bysort DEP ARR: carryforward XP1 XP0, replace

** drop routes at the end of the sample where no future departure is found for the non-PC route
drop if A_time1new==. | A_time0new==.

g tt0=A_time0new-D0_time
g tt1=A_time1new-D0_time

** travel time using only routes that passed in original dataset
g tt0_XP=tt0 if XP0==1
g tt1_XP=tt1 if XP0==1

g Dtt_XP=tt1_XP-tt0_XP
g Dtt_XP_pc=(tt1_XP-tt0_XP)/tt0_XP


collapse (mean) tt* mDtt_XP=Dtt_XP mDtt_XP_pc=Dtt_XP_pc (count) Ntt0=tt0 Ntt0_XP=tt0_XP, by(DEP ARR)

g Dtt=tt1-tt0
g Dtt_XP=tt1_XP-tt0_XP

if `p'>1 {
append using "$data/tt_NP_pre"
}
save "$data/tt_NP_pre", replace
}

}



** produce summary statistics on additional days needed to avoid Panama Canal



u "$data/tt_NP_pre", clear
rename DEP D_acid 
rename ARR A_acid
merge m:1 *acid using "$data/XPanama_byHrPRE514"
drop if _merge==2
drop _merge

*convert to days
g DDtt_XP=Dtt_XP/24

*bunch everything above 50
g DDtt_XP4plot=DDtt_XP
replace DDtt_XP4 = 50 if DDtt_XP4 >50


hist DDtt_XP4pl  [w=Ntt0_XP], width(1) xlabel(0(5)45 50 ">=50") color(gs10) fcolor(gs11) graphregion(color(white)) xtitle("Additional Days of Travel without the Panama Canal") ytitle("share of optimal paths")
graph export "$figures/hist_noPanama.png", replace


egen NN=total(Ntt0_XP) if DDtt_XP>3
egen NNN=total(Ntt0_XP)

summ NN
global NN=r(mean)
summ NNN
di $NN/r(mean)
* 93.1% take 3 days or more

g DDpc=DDtt_XP/Dtt_XP  
summ DDpc [w=Ntt0_XP] if XPa>0
summ DDpc [w=Ntt0_XP]

replace mDtt_XP=mDtt_XP/24

summ mDtt_XP [w=Ntt0_XP] if XPa>0
summ mDtt_XP [w=Ntt0_XP]
* 14 days

summ mDtt_XP_pc [w=Ntt0_XP] if XPa>0
summ mDtt_XP_pc [w=Ntt0_XP]














